package cn.iocoder.yudao.module.cmt.service.base;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.base.BaseCommunityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.cmt.dal.mysql.base.BaseCommunityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

/**
 * 社区信息 Service 实现类
 *
 * @author LRC
 */
@Service
@Validated
public class BaseCommunityServiceImpl implements BaseCommunityService {

    @Resource
    private BaseCommunityMapper baseCommunityMapper;

    @Override
    public Long createBaseCommunity(BaseCommunitySaveReqVO createReqVO) {
        // 插入
        BaseCommunityDO baseCommunity = BeanUtils.toBean(createReqVO, BaseCommunityDO.class);
        // 根据 areaId 填充省市区
        fillAreaInfo(baseCommunity);
        baseCommunityMapper.insert(baseCommunity);

        // 返回
        return baseCommunity.getId();
    }

    @Override
    public void updateBaseCommunity(BaseCommunitySaveReqVO updateReqVO) {
        // 校验存在
        validateBaseCommunityExists(updateReqVO.getId());
        // 更新
        BaseCommunityDO updateObj = BeanUtils.toBean(updateReqVO, BaseCommunityDO.class);
        // 根据 areaId 填充省市区
        fillAreaInfo(updateObj);
        baseCommunityMapper.updateById(updateObj);
    }

    /**
     * 根据 areaId 填充省市区信息
     *
     * @param baseCommunity 社区信息
     */
    private void fillAreaInfo(BaseCommunityDO baseCommunity) {
        if (baseCommunity.getAreaId() == null) {
            return;
        }

        // 获取地区信息
        Area area = AreaUtils.getArea(baseCommunity.getAreaId());
        if (area == null) {
            return;
        }

        // 根据地区类型填充不同字段
        Integer provinceId = AreaUtils.getParentIdByType(baseCommunity.getAreaId(), AreaTypeEnum.PROVINCE);
        Integer cityId = AreaUtils.getParentIdByType(baseCommunity.getAreaId(), AreaTypeEnum.CITY);
        Integer districtId = AreaUtils.getParentIdByType(baseCommunity.getAreaId(), AreaTypeEnum.DISTRICT);

        // 填充省份
        if (provinceId != null) {
            Area province = AreaUtils.getArea(provinceId);
            if (province != null) {
                baseCommunity.setProvince(province.getName());
            }
        }

        // 填充城市
        if (cityId != null) {
            Area city = AreaUtils.getArea(cityId);
            if (city != null) {
                baseCommunity.setCity(city.getName());
            }
        }

        // 填充区县
        if (districtId != null) {
            Area district = AreaUtils.getArea(districtId);
            if (district != null) {
                baseCommunity.setDistrict(district.getName());
            }
        }

        // 如果 areaId 本身就是区县,则直接使用
        if (AreaTypeEnum.DISTRICT.getType().equals(area.getType())) {
            baseCommunity.setDistrict(area.getName());
        }
    }

    @Override
    public void deleteBaseCommunity(Long id) {
        // 校验存在
        validateBaseCommunityExists(id);
        // 删除
        baseCommunityMapper.deleteById(id);
    }

    @Override
        public void deleteBaseCommunityListByIds(List<Long> ids) {
        // 删除
        baseCommunityMapper.deleteByIds(ids);
        }


    private void validateBaseCommunityExists(Long id) {
        if (baseCommunityMapper.selectById(id) == null) {
            throw exception(BASE_COMMUNITY_NOT_EXISTS);
        }
    }

    @Override
    public BaseCommunityDO getBaseCommunity(Long id) {
        return baseCommunityMapper.selectById(id);
    }

    @Override
    public PageResult<BaseCommunityDO> getBaseCommunityPage(BaseCommunityPageReqVO pageReqVO) {
        return baseCommunityMapper.selectPage(pageReqVO);
    }

}